<?php

/**
 * 
 * @project     : innoTweets 1.0
 * @author      : Dino (DBK)
 * 
 * @package     : Settings Model
 * @description : Database functions related to innoTweets timeline
 * 
 */
class Timeline_model extends CI_Model {

  function __construct() {
	parent::__construct();
  }

  // Get new tweets
  function get_new_tweets($date = NULL, $limit = 25, $offset = NULL) {
	$result_data = array();
	$result_data['status'] = 0;
	$result_data['result'] = '';
	$result_data['count'] = 0;
	$query_cond = array();
	$query_cond['user_status'] = 1;
	$query_cond['tweet_status'] = 1;
	$query_cond['Date(tweet_date)'] = $date;
	$this->db->join('users', 'user_id = tweet_user_id');
	$this->db->order_by('tweet_id', 'desc');
	$query_exec = $this->db->get_where('tweets', $query_cond, $limit, $offset);
	if ($query_exec == TRUE) {
	  $result_data = array();
	  $result_data['status'] = 1;
	  $result_data['result'] = $query_exec->result_array();
	  $result_data['count'] = $query_exec->num_rows();
	}
	return $result_data;
  }

  // Get new replies
  function get_new_replies($date = NULL, $limit = 25, $offset = NULL) {
	$result_data = array();
	$result_data['status'] = 0;
	$result_data['result'] = '';
	$result_data['count'] = 0;
	$query_cond = array();
	$query_cond['user_status'] = 1;
	$query_cond['tweet_reply_status'] = 1;
	$query_cond['Date(tweet_reply_date)'] = $date;
	$this->db->join('users', 'user_id = tweet_reply_user_id');
	$this->db->order_by('tweet_reply_id', 'desc');
	$query_exec = $this->db->get_where('tweets_reply', $query_cond, $limit, $offset);
	if ($query_exec == TRUE) {
	  $result_data = array();
	  $result_data['status'] = 1;
	  $result_data['result'] = $query_exec->result_array();
	  $result_data['count'] = $query_exec->num_rows();
	}
	return $result_data;
  }

  // Get new events
  function get_new_events($date = NULL, $limit = 25, $offset = NULL) {
	$result_data = array();
	$result_data['status'] = 0;
	$result_data['result'] = '';
	$result_data['count'] = 0;
	$query_cond = array();
	$query_cond['user_status'] = 1;
	$query_cond['event_status'] = 1;
	$query_cond['Date(event_date)'] = $date;
	$this->db->join('users', 'user_id = event_user_id');
	$this->db->order_by('event_date', 'desc');
	$query_exec = $this->db->get_where('events', $query_cond, $limit, $offset);
	if ($query_exec == TRUE) {
	  $result_data = array();
	  $result_data['status'] = 1;
	  $result_data['result'] = $query_exec->result_array();
	  $result_data['count'] = $query_exec->num_rows();
	}
	return $result_data;
  }

  // Get new albums
  function get_new_albums($date = NULL, $limit = 25, $offset = NULL) {
	$result_data = array();
	$result_data['status'] = 0;
	$result_data['result'] = '';
	$result_data['count'] = 0;
	$query_cond = array();
	$query_cond['user_status'] = 1;
	$query_cond['album_status'] = 1;
	$query_cond['Date(album_date)'] = $date;
	$this->db->join('users', 'user_id = album_user_id');
	$this->db->order_by('album_id', 'desc');
	$query_exec = $this->db->get_where('albums', $query_cond, $limit, $offset);
	if ($query_exec == TRUE) {
	  $result_data['status'] = 1;
	  $result_data['result'] = $query_exec->result_array();
	  $result_data['count'] = $query_exec->num_rows();
	}

	return $result_data;
  }

  // Get new likes
  function get_new_likes($date = NULL, $limit = 25, $offset = NULL) {
	$result_data = array();
	$result_data['status'] = 0;
	$result_data['result'] = '';
	$result_data['count'] = 0;
	$query_cond = array();
	$query_cond['user_status'] = 1;
	$query_cond['Date(like_date)'] = $date;
	$this->db->join('users', 'user_id = like_user_id');
	$this->db->order_by('like_date', 'desc');
	$query_exec = $this->db->get_where('likes', $query_cond, $limit, $offset);
	if ($query_exec == TRUE) {
	  $result_data = array();
	  $result_data['status'] = 1;
	  $result_data['result'] = $query_exec->result_array();
	  $result_data['count'] = $query_exec->num_rows();
	}
	return $result_data;
  }

  // Get new wall posts
  function get_new_wall_posts($date = NULL, $limit = 25, $offset = NULL) {
	$result_data = array();
	$result_data['status'] = 0;
	$result_data['result'] = '';
	$result_data['count'] = 0;
	$query_cond = array();
	$query_cond['user_status'] = 1;
	$query_cond['wall_post_status'] = 1;
	$query_cond['Date(wall_post_date)'] = $date;
	$this->db->join('users', 'user_id = wall_post_from_user_id');
	$this->db->order_by('wall_post_id', 'desc');
	$query_exec = $this->db->get_where('wall_posts', $query_cond, $limit, $offset);
	if ($query_exec == TRUE) {
	  $result_data = array();
	  $result_data['status'] = 1;
	  $result_data['result'] = $query_exec->result_array();
	  $result_data['count'] = $query_exec->num_rows();
	}
	return $result_data;
  }

  // Get new polls
  function get_new_polls($date = NULL, $limit = 25, $offset = NULL) {
	$result_data = array();
	$result_data['status'] = 0;
	$result_data['result'] = '';
	$result_data['count'] = 0;
	$query_cond = array();
	$query_cond['user_status'] = 1;
	$query_cond['poll_status'] = 1;
	$query_cond['Date(poll_date)'] = $date;
	$this->db->join('users', 'user_id = poll_user_id');
	$this->db->order_by('poll_id', 'desc');
	$query_exec = $this->db->get_where('polls', $query_cond, $limit, $offset);
	if ($query_exec == TRUE) {
	  $result_data = array();
	  $result_data['status'] = 1;
	  $result_data['result'] = $query_exec->result_array();
	  $result_data['count'] = $query_exec->num_rows();
	}
	return $result_data;
  }

  // Get new poll votes
  function get_new_poll_votes($date = NULL, $limit = 25, $offset = NULL) {
	$result_data = array();
	$result_data['status'] = 0;
	$result_data['result'] = '';
	$result_data['count'] = 0;
	$query_cond = array();
	$query_cond['user_status'] = 1;
	$query_cond['poll_status'] = 1;
	$query_cond['Date(poll_reply_date)'] = $date;
	$this->db->join('polls', 'poll_id = poll_reply_poll_id');
	$this->db->join('users', 'user_id = poll_reply_user_id');
	$this->db->order_by('poll_reply_date', 'desc');
	$query_exec = $this->db->get_where('polls_reply', $query_cond, $limit, $offset);
	if ($query_exec == TRUE) {
	  $result_data['status'] = 1;
	  $result_data['result'] = $query_exec->result_array();
	  $result_data['count'] = $query_exec->num_rows();
	}
	return $result_data;
  }

  // Get new comments
  function get_new_comments($date = NULL, $limit = 25, $offset = NULL) {
	$result_data = array();
	$result_data['status'] = 0;
	$result_data['result'] = '';
	$result_data['count'] = 0;
	$query_cond = array();
	$query_cond['user_status'] = 1;
	$query_cond['comment_status'] = 1;
	$query_cond['Date(comment_date)'] = $date;
	$this->db->join('users', 'user_id = comment_user_id');
	$this->db->order_by('comment_id', 'desc');
	$query_exec = $this->db->get_where('comments', $query_cond, $limit, $offset);
	if ($query_exec == TRUE) {
	  $result_data = array();
	  $result_data['status'] = 1;
	  $result_data['result'] = $query_exec->result_array();
	  $result_data['count'] = $query_exec->num_rows();
	}

	return $result_data;
  }

  // Get album photos by album id
  function get_album_photos($album_id = NULL, $limit = NULL) {
	$result_data = array();
	$result_data['status'] = 0;
	$result_data['result'] = '';
	$result_data['count'] = 0;
	if (!empty($album_id) && is_numeric($album_id)) {
	  $query_cond = array();
	  $query_cond['photo_album_id'] = $album_id;
	  $this->db->order_by('photo_id', 'RANDOM');
	  $query_exec = $this->db->get_where('albums_photos', $query_cond, $limit);
	  if ($query_exec == TRUE) {
		$result_data = array();
		$result_data['status'] = 1;
		$result_data['result'] = $query_exec->result_array();
		$result_data['count'] = $query_exec->num_rows();
	  }
	}
	return $result_data;
  }

  // Get poll options by poll id
  function get_poll_options($poll_id = NULL) {
	$result_data = array();
	$result_data['status'] = 0;
	$result_data['result'] = '';
	$result_data['count'] = 0;
	if (!empty($poll_id) && is_numeric($poll_id)) {
	  $query_cond = array();
	  $query_cond['poll_option_poll_id'] = $poll_id;
	  $query_exec = $this->db->get_where('polls_options', $query_cond);
	  if ($query_exec == TRUE) {
		$result_data['status'] = 1;
		$result_data['result'] = $query_exec->result_array();
		$result_data['count'] = $query_exec->num_rows();
	  }
	}
	return $result_data;
  }

}

?>
